package com.lws.hellojmeter.http.controller;

import com.lws.hellojmeter.http.bean.HttpInfo;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2016/4/4.
 */
@Controller
@RequestMapping("/http.do")
public class HttpController {

    @Value("${jmeter.command}")
    private String command;

    @Value("${jmeter.template}")
    private String template;

    @RequestMapping(params = "method=testurl")
    public void testurl() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(params = "method=test")
    public HttpInfo test(HttpServletRequest request) {
        int threads = 0, cricle = 0;
        try {
            threads = Integer.parseInt(request.getParameter("threads"));
            cricle = Integer.parseInt(request.getParameter("cricle"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = request.getParameter("url");

        if (threads == 0 || cricle == 0 || StringUtils.isBlank(url)) {
            return null;
        }

        String[] cmdarray = new String[]{command, "-n",
                "-Jthreads=" + threads,
                "-Jurl=" + url,
                "-Jcricle=" + cricle,
                "-t", template, "-l", "bbb.log"};

        HttpInfo info = null;
        try {
            info = connet(cmdarray);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }

    private HttpInfo connet(String[] cmdarray) throws IOException {
        Process p;
        try {
            p = Runtime.getRuntime().exec(cmdarray);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }

        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        HttpInfo info = null;
        try {
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("summary =")) {
                    String[] words = line.split("\\s+");
                    int sum = Integer.parseInt(words[2]);
                    float rate = Float.parseFloat(words[6].split("/")[0]);
                    int avg = Integer.parseInt(words[8]);
                    int min = Integer.parseInt(words[10]);
                    int max = Integer.parseInt(words[12]);
                    int err = Integer.parseInt(words[14]);
                    info = new HttpInfo(sum, rate, avg, min, max, err);
                    System.out.println(info);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(reader);
        }
        p.destroy();
        return info;
    }

}
